package com.crm.system.service.impl.csd.customerManager;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.qo.exchangeProductsRecord.ProductExchangeFormQO;
import com.crm.model.qo.exchangeProductsRecord.ProductExchangeaApprovalQO;
import com.crm.model.qo.finishOrderProductManager.FinishOrderProductQO;
import com.crm.model.vo.csd.product.ChangeCourseRecordVO;
import com.crm.model.vo.customerContractManager.CustomerChangeCourseRecordVO;
import com.crm.model.vo.exchangeProductsRecord.*;
import com.crm.model.vo.finishOrderProduct.FinishOrderProductVO;
import com.crm.model.vo.productManager.CustomerMemberProductVO;
import com.crm.model.vo.productManager.CustomerProductVO;
import com.crm.service.csd.customerManager.ProductDetailsDbService;
import com.crm.service.customerManager.CustomerDbService;
import com.crm.service.sys.permissionManagement.SysCompanyDbService;
import com.crm.system.service.csd.customerManager.ProductDetailsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangpeng
 * @Date 2021/9/1 13:50
 */
@Service
public class ProductDetailsServiceImpl implements ProductDetailsService {

    @Autowired
    private ProductDetailsDbService productDetailsDbService;
    @Autowired
    SysCompanyDbService sysCompanyDbService;
    @Autowired
    CustomerDbService customerDbService;
    /**
     * selectProductDetailsList
     *  产品管理 --产品明细
     * @Author wangpeng
     * @Date 2021/9/1
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param qo
     * @return
     */
    @Override
    public DhPageInfo<FinishOrderProductVO> selectProductDetailsList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, FinishOrderProductQO qo) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("f.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        Integer dateUnit = 0;
        if (!StringUtils.isBlank(qo.getDateType())) {
            if ("day".equals(qo.getDateType())) {
                //今天
                dateUnit = 1;
            }
            if ("thisWeek".equals(qo.getDateType())) {
                //本周
                dateUnit = 2;
            }
            if ("thisMonth".equals(qo.getDateType())) {
                //本月
                dateUnit = 3;
            }
            if ("lastMonth".equals(qo.getDateType())) {
                //上月
                dateUnit = 4;
            }

        }
        String contractOtherType =null;
        List<String> orderDetailType = qo.getOrderDetailType();
        for(int i =0;i<orderDetailType.size();i++){
            String s = orderDetailType.get(i);
            if (s.equals("5")){
                contractOtherType = "1";
            }
        }
        List<FinishOrderProductVO> retustList=productDetailsDbService.selectProductDetailsList(order.toString(),qo,dateUnit,contractOtherType);
        PageInfo<FinishOrderProductVO> page =new PageInfo(retustList);
        return new DhPageInfo<FinishOrderProductVO>(page,retustList);
    }

    /**
     * selectCustomerMemberProductList
     * 产品管理--产品明细--会员高级筛选
     * @Author wangpeng
     * @Date 2021/9/2
     * @param productStatus
     * @return
     */
    @Override
    public List<CustomerMemberProductVO> selectCustomerMemberProductList(Integer productStatus) {
        return productDetailsDbService.selectCustomerMemberProductList(productStatus);
    }

    /**
     * selectCustomerProductList
     * 产品管理--产品明细--非会员高级筛选
     * @Author wangpeng
     * @Date 2021/9/3
     * @param shelfStatus
     * @return
     */
    @Override
    public List<CustomerProductVO> selectCustomerProductList(Integer shelfStatus) {
        return productDetailsDbService.selectCustomerProductList(shelfStatus);
    }

    /**
     * selectChangeCourseCecordList
     * @Author wangpeng
     * @Date 2021/10/22
     * @param pageIndex	
     * @param pageSize	
     * @param sortName	
     * @param sortOrder	
     * @return
     */
    @Override
    public DhPageInfo<CustomerChangeCourseRecordVO> selectChangeCourseCecordList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, ChangeCourseRecordVO vo) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<CustomerChangeCourseRecordVO> retustList=productDetailsDbService.selectChangeCourseCecordList(order.toString(),vo);
        PageInfo<CustomerChangeCourseRecordVO> page =new PageInfo(retustList);
        return new DhPageInfo<CustomerChangeCourseRecordVO>(page,retustList);
    }

    @Override
    public List<ChangeCourseRecordVO> selectChangeCourseCecordRuleNumber() {
        return productDetailsDbService.selectChangeCourseCecordRuleNumber();
    }

    @Override
    public List<CustomerChangeCourseRecordVO> selectExamineAndApproveList(String changeCourseId) {
        CustomerChangeCourseRecordVO record =productDetailsDbService.selectChangeCourseCecordById(changeCourseId);
        List<CustomerChangeCourseRecordVO> retustList=productDetailsDbService.selectExamineAndApproveList(record.getFinishOrderProductId());
        return retustList;
    }

    @Override
    public Map exchangeApproval(String changeCourseId, String examine,String remarks,String[] courseId,String[] changeCourseTitle,String ruleTitle) {
        HashMap<Object, Object> map = new HashMap<>();
        CustomerChangeCourseRecordVO record =productDetailsDbService.selectChangeCourseCecordById(changeCourseId);
        String changeCourseType = record.getChangeCourseType();
        //当前时间
        Date date = new Date();
        //获取用户信息
        UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        SysCompany sysCompany = sysCompanyDbService.selectSysCompanyById(user.getCompanyId());
        //现场天数
        if("0".equals(changeCourseType)) {
            Double xcchts = Double.valueOf(record.getXcch());
            Double xcchKc = Double.valueOf(record.getXcchKc());
            if (examine.equals("0")) {
                HashMap<String, Object> paramMap = new HashMap<>();
                Double xc = xcchts - xcchKc;
                paramMap.put("card", record.getMemberCardNumber());
                paramMap.put("num1", xc);
                paramMap.put("pid", sysCompany.getCompanyCode());
                paramMap.put("pname", sysCompany.getCompanyName());
                paramMap.put("tuname", user.getUserName());
                paramMap.put("remarks",ruleTitle);
                String res1 = HttpUtil.post("http://api.nsrjlb.com/crm/MemCard/memberCardDeductionForCrm", paramMap);
                JSONObject json1 = JSON.parseObject(res1);
                Integer de = json1.getInteger("code");
                if (de != null && de == 1) {
                    String hyhsycs = null;
                    //修改状态
                    productDetailsDbService.updateChangeCourseCecord(changeCourseId,examine,remarks,user.getAccountId(),date, String.valueOf(xc),hyhsycs);
                    customerDbService.updatechsCustomerMemberCardDetail(hyhsycs, record.getXcch(), user.getAccountId(), record.getMemberCardNumber(), record.getCustomerProductDetailMemberCardId());
                    CustomerChangeCourseRecordVO vo =productDetailsDbService.selectChangeCourseCecordById(changeCourseId);
                    addFinishOrderProduct(vo.getChangeCourseTitle(), vo.getCourseProductCode(), vo.getAccountId(), vo.getFinishOrderProductId(), vo.getProductId(), vo.getCompanyId());
                }else{
                    map.put("code", 1);
                    map.put("msg", "因系统原因,该条记录未通过");
                    return map;
                }
            }else {
                String xcch = String.valueOf(xcchts);
                String hyhsycs = null;
                productDetailsDbService.updateChangeCourseCecord(changeCourseId,examine,remarks,user.getAccountId(),date,xcch,hyhsycs);
            }
        }
        //行业会
        if("1".equals(changeCourseType)) {
            Double hyhsy = Double.valueOf(record.getHyhsycs());
            Double hyhsycsKc = Double.valueOf(record.getHyhsycsKc());
            if (examine.equals("0")) {
                HashMap<String, Object> paramMap = new HashMap<>();
                Double hyh = hyhsy - hyhsycsKc;
                paramMap.put("card", record.getMemberCardNumber());
                paramMap.put("num7", hyh);
                paramMap.put("pid", sysCompany.getCompanyCode());
                paramMap.put("pname", sysCompany.getCompanyName());
                paramMap.put("tuname", user.getUserName());
                paramMap.put("remarks", ruleTitle);
                String res1 = HttpUtil.post("http://api.nsrjlb.com/crm/MemCard/memberCardDeductionForCrm", paramMap);
                JSONObject json1 = JSON.parseObject(res1);
                Integer de = json1.getInteger("code");
                if (de != null && de == 1) {
                    String xcch = null;
                    //修改状态
                    productDetailsDbService.updateChangeCourseCecord(changeCourseId,examine,remarks,user.getAccountId(),date,xcch, String.valueOf(hyh));
                    customerDbService.updatechsCustomerMemberCardDetail(record.getHyhsycs(),xcch,user.getAccountId(), record.getMemberCardNumber(), record.getCustomerProductDetailMemberCardId());
                    CustomerChangeCourseRecordVO vo =productDetailsDbService.selectChangeCourseCecordById(changeCourseId);
                    addFinishOrderProduct(vo.getChangeCourseTitle(), vo.getCourseProductCode(), vo.getAccountId(), vo.getFinishOrderProductId(), vo.getProductId(), vo.getCompanyId());
                }else{
                    map.put("code", 1);
                    map.put("msg", "因系统原因,该条记录未通过");
                    return map;
                }
            }else {
                String xcch = null;
                String hyhsycs =  String.valueOf(hyhsy);
                productDetailsDbService.updateChangeCourseCecord(changeCourseId,examine,remarks,user.getAccountId(),date,xcch,hyhsycs);
            }
        }
        //高端班
        if("2".equals(changeCourseType)) {
            String xcch = null;
            String hyhsycs = null;
            if (examine.equals("0")) {
                HashMap<String, Object> paramMap = new HashMap<>();
                paramMap.put("card", record.getMemberCardNumber());
                paramMap.put("is_vipnum", 0);
                paramMap.put("pid", sysCompany.getCompanyCode());
                paramMap.put("pname", sysCompany.getCompanyName());
                paramMap.put("tuname", user.getUserName());
                paramMap.put("remarks", changeCourseTitle);
                String res1 = HttpUtil.post("http://api.nsrjlb.com/crm/MemCard/memberCardDeductionForCrm", paramMap);
                JSONObject json1 = JSON.parseObject(res1);
                Integer de = json1.getInteger("code");
                if (de != null && de == 1) {
                    //修改状态
                    for (int i= 0;i<courseId.length;i++){
                        productDetailsDbService.updateChangeCourseCecord(courseId[i],examine,remarks,user.getAccountId(),date,xcch,hyhsycs);
                        CustomerChangeCourseRecordVO vo =productDetailsDbService.selectChangeCourseCecordById(courseId[i]);
                        addFinishOrderProduct(vo.getChangeCourseTitle(), vo.getCourseProductCode(), vo.getAccountId(), vo.getFinishOrderProductId(), vo.getProductId(), vo.getCompanyId());
                    }
                }else{
                    map.put("code", 1);
                    map.put("msg", "因系统原因,该条记录未通过");
                    return map;
                }
            }else {
                for (int i= 0;i<courseId.length;i++) {
                    productDetailsDbService.updateChangeCourseCecord(courseId[i], examine, remarks,user.getAccountId(),date,xcch,hyhsycs);
                }
            }
        }
        //高端班兑换现场天数
        if("3".equals(changeCourseType)) {
            if (examine.equals("0")) {
                Double xcch1 = Double.valueOf(record.getXcch());
                Double gdbChangeXcch = Double.valueOf(record.getGdbChangeXcch());
                //现场天数
                String xcch = String.valueOf(xcch1 + gdbChangeXcch);
                HashMap<String, Object> paramMap = new HashMap<>();
                paramMap.put("card", record.getMemberCardNumber());
                paramMap.put("num1", xcch);
                paramMap.put("is_vipnum", 0);
                paramMap.put("pid", sysCompany.getCompanyCode());
                paramMap.put("pname", sysCompany.getCompanyName());
                paramMap.put("tuname", user.getUserName());
                paramMap.put("remarks", ruleTitle);
                String res1 = HttpUtil.post("http://api.nsrjlb.com/crm/MemCard/memberCardDeductionForCrm", paramMap);
                JSONObject json1 = JSON.parseObject(res1);
                Integer de = json1.getInteger("code");
                if (de != null && de == 1) {
                    String hyhsycs = null;
                    //修改状态
                    productDetailsDbService.updateChangeCourseCecord(changeCourseId,examine,remarks,user.getAccountId(),date,xcch,hyhsycs);
                    customerDbService.updatechsCustomerMemberCardDetail(hyhsycs,xcch,user.getAccountId(), record.getMemberCardNumber(), record.getCustomerProductDetailMemberCardId());
                }else{
                    map.put("code", 1);
                    map.put("msg", "因系统原因,该条记录未通过");
                    return map;
                }
            }else {
                String xcch = null;
                String hyhsycs = null;
                productDetailsDbService.updateChangeCourseCecord(changeCourseId,examine,remarks,user.getAccountId(),date,xcch,hyhsycs);
            }
        }

        //现场天数兑换行业会
        if("4".equals(changeCourseType)) {
            if (examine.equals("0")) {
                Double xcch1 = Double.valueOf(record.getXcch());
                Double hyhsycs1 = Double.valueOf(record.getHyhsycs());
                Double xcchKc = Double.valueOf(record.getXcchKc());
                Double hyhsycsKc = Double.valueOf(record.getHyhsycsKc());
                //现场减天数， 行业会增加天数
                String xcch = String.valueOf(xcch1 -xcchKc);
                String hyhsycs = String.valueOf(hyhsycs1 + hyhsycsKc);
                HashMap<String, Object> paramMap = new HashMap<>();
                paramMap.put("card", record.getMemberCardNumber());
                paramMap.put("num1", xcch);
                paramMap.put("num7", hyhsycs);
                paramMap.put("pid", sysCompany.getCompanyCode());
                paramMap.put("pname", sysCompany.getCompanyName());
                paramMap.put("tuname", user.getUserName());
                paramMap.put("remarks", ruleTitle);
                String res1 = HttpUtil.post("http://api.nsrjlb.com/crm/MemCard/memberCardDeductionForCrm", paramMap);
                JSONObject json1 = JSON.parseObject(res1);
                Integer de = json1.getInteger("code");
                if (de != null && de == 1) {
                    //修改状态
                    productDetailsDbService.updateChangeCourseCecord(changeCourseId,examine,remarks,user.getAccountId(),date,xcch,hyhsycs);
                    customerDbService.updatechsCustomerMemberCardDetail(hyhsycs,xcch,user.getAccountId(), record.getMemberCardNumber(), record.getCustomerProductDetailMemberCardId());
                }else{
                    map.put("code", 1);
                    map.put("msg", "因系统原因,该条记录未通过");
                    return map;
                }
            }else {
                String xcch = null;
                String hyhsycs = null;
                productDetailsDbService.updateChangeCourseCecord(changeCourseId,examine,remarks,user.getAccountId(),date,xcch,hyhsycs);
            }
        }

        //行业会次数兑换现场天数
        if("5".equals(changeCourseType)) {
            if (examine.equals("0")) {
                Double xcch1 = Double.valueOf(record.getXcch());
                Double hyhsycs1 = Double.valueOf(record.getHyhsycs());
                Double xcchKc = Double.valueOf(record.getXcchKc());
                Double hyhsycsKc = Double.valueOf(record.getHyhsycsKc());
                //行业会减次数， 现场增加天数
                String xcch = String.valueOf(xcch1 + xcchKc);
                String hyhsycs = String.valueOf(hyhsycs1 - hyhsycsKc);
                HashMap<String, Object> paramMap = new HashMap<>();
                paramMap.put("card", record.getMemberCardNumber());
                paramMap.put("num1", xcch);
                paramMap.put("num7", hyhsycs);
                paramMap.put("pid", sysCompany.getCompanyCode());
                paramMap.put("pname", sysCompany.getCompanyName());
                paramMap.put("tuname", user.getUserName());
                paramMap.put("remarks", ruleTitle);
                String res1 = HttpUtil.post("http://api.nsrjlb.com/crm/MemCard/memberCardDeductionForCrm", paramMap);
                JSONObject json1 = JSON.parseObject(res1);
                Integer de = json1.getInteger("code");
                if (de != null && de == 1) {
                    //修改状态
                    productDetailsDbService.updateChangeCourseCecord(changeCourseId,examine,remarks,user.getAccountId(),date,xcch,hyhsycs);
                    customerDbService.updatechsCustomerMemberCardDetail(hyhsycs,xcch,user.getAccountId(), record.getMemberCardNumber(), record.getCustomerProductDetailMemberCardId());
                }else{
                    map.put("code", 1);
                    map.put("msg", "因系统原因,该条记录未通过");
                    return map;
                }
            }else {
                String xcch = null;
                String hyhsycs = null;
                productDetailsDbService.updateChangeCourseCecord(changeCourseId,examine,remarks,user.getAccountId(),date,xcch,hyhsycs);
            }
        }

        map.put("code", 0);
        map.put("msg", "成功");
        return map;
    }

    @Override
    public DhPageInfo<CustomerChangeCourseRecordVO> selectExchangeCenterList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, ChangeCourseRecordVO vo) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        String accountId = user.getAccountId();
        List<CustomerChangeCourseRecordVO> retustList=productDetailsDbService.selectExchangeCenterList(order.toString(),vo,accountId);
        PageInfo<CustomerChangeCourseRecordVO> page =new PageInfo(retustList);
        return new DhPageInfo<CustomerChangeCourseRecordVO>(page,retustList);
    }

    @Override
    public ChangeCourseRecordVO selectExchangeDetails(String changeCourseId) {
        return productDetailsDbService.selectExchangeDetails(changeCourseId);
    }

    @Override
    public CustomerChangeCourseRecordVO selectViewDetails(String changeCourseId) {
        return productDetailsDbService.selectViewDetails(changeCourseId);
    }

    @Override
    public DhPageInfo<FinishOrderProductVO> selectExchangeOrderProductList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String orderNumber) {
            if (pageIndex != null && pageSize != null) {
                PageHelper.startPage(pageIndex, pageSize);
            }
            //排序
            StringBuilder order = new StringBuilder();
            if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
                switch (sortName) {
                    case "createTm": {
                        order.append("t.create_tm ").append(sortOrder);//创建时间
                    }
                    break;
                    default:
                        break;
                }
            }
            UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
            String accountId = user.getAccountId();
            List<FinishOrderProductVO> retustList=productDetailsDbService.selectExchangeOrderProductList(order.toString(),orderNumber,accountId);
            PageInfo<FinishOrderProductVO> page =new PageInfo(retustList);
            return new DhPageInfo<FinishOrderProductVO>(page,retustList);
        }

    @Override
    public List<FinishOrderProductVO> selectExchangeOrderProductByorderNumber(String orderNumber) {
        UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        String accountId = user.getAccountId();
        return productDetailsDbService.selectExchangeOrderProductByorderNumber(orderNumber,accountId);
    }


    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Map addProductExchangeApproval(ProductExchangeaApprovalQO qo) {
        HashMap<Object, Object> map = new HashMap<>();
        UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        //获取当前时间
        Date date = new Date();
        //6位数编码
        String number = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        List<FinishOrderProductVO> list =  qo.getFinishOrderProductList();
        List<ProductExchangeFormVO> list2 =qo.getProductExchangeApprovalList();
        if (list.size() != 0){
            for (int j = 0; j < list.size(); j++) {
                for (int i = 0; i < list2.size(); i++) {
                    ProductExchangeFormVO form = list2.get(i);
                    FinishOrderProductVO approval = list.get(j);
                    ExchangeProductsRecordVO vo = new ExchangeProductsRecordVO();
                    //调用方法取uuid
                    String guid = CommonUtils.getGUID();
                    vo.setExchangeProductsId(guid);
                    vo.setOrderNumber(qo.getOrderNumber());
                    vo.setProductId(form.getProductId());
                    vo.setProductName(form.getProductName());
                    vo.setProductOriginalPrice(form.getProductOriginalPrice());
                    vo.setProductMemberPrice(form.getProductMemberPrice());
                    vo.setProductUnitPrice(form.getProductUnitPrice());
                    vo.setPrice(form.getPrice());
                    vo.setProductType(form.getProductType());
                    vo.setSellingCardPrice(form.getSellingCardPrice());
                    vo.setProductExchangeId(form.getProductExchangeId());
                    vo.setExchangeProductName(approval.getProductName());
                    vo.setExchangeFinishOrderProductId(approval.getFinishOrderProductId());
                    vo.setProductFinalPrice(String.valueOf(approval.getProductFinalPrice()));
                    vo.setFinishOrderProductNum(approval.getFinishOrderProductNum());
                    vo.setAccountId(user.getAccountId());
                    vo.setCompanyId(user.getCompanyId());
                    vo.setCreateTm(date);
                    vo.setExchangeNumber(number);
                    productDetailsDbService.addExchangeProductsRecord(vo);
                }
            }
        }
        //调用方法取uuid
        String id = CommonUtils.getGUID();
        //获取当前用户
        ProductExchangeaApprovalVO vo = new ProductExchangeaApprovalVO();
        vo.setProductExchangeApprovalId(id);
        vo.setOrderNumber(qo.getOrderNumber());
        vo.setRemarks(qo.getRemarks());
        vo.setUploadId(qo.getUploadId());
        vo.setAccountId(user.getAccountId());
        vo.setApprovalStatus("1");
        vo.setExchangeNumber(number);
        vo.setCreateTm(date);
        vo.setCompanyId(user.getCompanyId());
        vo.setUserName(user.getUserName());
        vo.setContractNumber(qo.getContractNumber());
        vo.setCustomerName(qo.getCustomerName());
        vo.setProductStatus(qo.getProductStatus());
        productDetailsDbService.addProductExchangeApproval(vo);
        productDetailsDbService.updateProductExchangeForm(qo.getOrderNumber());
        map.put("code", 0);
        map.put("msg", "审批提交成功");
        return map;
    }

    @Override
    public DhPageInfo<ProductExchangeaApprovalVO> selectProductExchangeApprovalList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);//创建时间
                }
                break;
                default:
                    break;
            }
        }
        List<ProductExchangeaApprovalVO> retustList=productDetailsDbService.selectProductExchangeApprovalList(order.toString());
        PageInfo<ProductExchangeaApprovalVO> page =new PageInfo(retustList);
        return new DhPageInfo<ProductExchangeaApprovalVO>(page,retustList);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void updateProductExchangeApproval(String productExchangeApprovalId,String approvalStatus,String approvalRemarks,String exchangeFinishOrderProductId,String exchangeNumber) {
        if(approvalStatus.equals("0")){
            String type ="1";
            productDetailsDbService.updateExchangeProductsRecord(exchangeNumber,type);
            String[] split = exchangeFinishOrderProductId.split(",");
            for(int i=0;i< split.length;i++){
                productDetailsDbService.updateFinishOrderProduct(split[i]);
            }
       }
        String type ="2";
        productDetailsDbService.updateExchangeProductsRecord(exchangeNumber,type);
        Date date = new Date();
        UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        productDetailsDbService.updateProductExchangeApproval(productExchangeApprovalId,approvalStatus,date,approvalRemarks,user.getAccountId());
    }

    @Override
    public DhPageInfo<ExchangeProductsRecordVO> selectProductExchangeApprovalByExchangeNumber(String exchangeNumber,String type) {
        List<ExchangeProductsRecordVO> retustList;
        if(type.equals("1")){
            retustList=productDetailsDbService.selectProductExchangeApprovalByType(exchangeNumber);
        }else {
           retustList=productDetailsDbService.selectProductExchangeApprovalByExchangeNumber(exchangeNumber);
        }
        PageInfo<ExchangeProductsRecordVO> page =new PageInfo(retustList);
        return new DhPageInfo<ExchangeProductsRecordVO>(page,retustList);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void addProductExchangeform(ProductExchangeFormQO qo) {
        //获取当前时间
        Date date = new Date();
        List<ExchangeCstomerProduct> list = qo.getList();
        List<ExchangeCstomerMemberProduct> memberProductList = qo.getMemberProductList();
        if(memberProductList.size() != 0){
            for(int j = 0; j < memberProductList.size(); j++) {
                ExchangeCstomerMemberProduct memberProduct = memberProductList.get(j);
                String guid = CommonUtils.getGUID();
                ProductExchangeFormVO  vo= new ProductExchangeFormVO();
                vo.setProductExchangeId(guid);
                vo.setProductName(memberProduct.getMemberCardName());
                if(String.valueOf(memberProduct.getSellingCardPrice()).equals("null")){
                    vo.setSellingCardPrice("");
                }else {
                    vo.setSellingCardPrice(String.valueOf(memberProduct.getSellingCardPrice()));
                }
                vo.setDelFlag("1");
                vo.setCreateTm(date);
                vo.setProductId(memberProduct.getMemberProductId());
                vo.setPrice(memberProduct.getPrice());
                vo.setProductCode(memberProduct.getMemberProductCode());
                vo.setOrderNumber(qo.getOrderNumber());
                vo.setProductType("1");
                UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
                String accountId = user.getAccountId();
                vo.setUserId(accountId);
                productDetailsDbService.addProductExchangeform(vo);
            }
        }
        if(list.size() != 0){
            for (int i = 0; i < list.size(); i++) {
                ExchangeCstomerProduct form = list.get(i);
                String guid = CommonUtils.getGUID();
                ProductExchangeFormVO vo = new ProductExchangeFormVO();
                vo.setProductExchangeId(guid);
                vo.setProductName(form.getProductName());
                if(String.valueOf(form.getProductMemberPrice()).equals("null")){
                    vo.setProductMemberPrice("");
                }else {
                    vo.setProductMemberPrice(String.valueOf(form.getProductMemberPrice()));
                }
                if(String.valueOf(form.getProductUnitPrice()).equals("null")){
                    vo.setProductUnitPrice("");
                }else {
                    vo.setProductUnitPrice(String.valueOf(form.getProductUnitPrice()));
                }
                if(String.valueOf(form.getProductOriginalPrice()).equals("null")){
                    vo.setProductOriginalPrice("");
                }else {
                    vo.setProductOriginalPrice(String.valueOf(form.getProductOriginalPrice()));
                }
                vo.setDelFlag("1");
                vo.setCreateTm(date);
                vo.setProductId(form.getCustomerProductId());
                vo.setPrice(form.getPrice());
                vo.setProductCode(form.getProductCode());
                vo.setOrderNumber(qo.getOrderNumber());
                vo.setProductType("2");
                UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
                String accountId = user.getAccountId();
                vo.setUserId(accountId);
                productDetailsDbService.addProductExchangeform(vo);

            }
        }
    }

    @Override
    public List<ProductExchangeFormVO> selectProductExchangeFormList(String orderNumber) {
        UserAccount user = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        String accountId = user.getAccountId();
        List<ProductExchangeFormVO> retustList=productDetailsDbService.selectProductExchangeFormList(orderNumber,accountId);
        return retustList;
    }

    @Override
    public Map deleteProductExchangeFormByProductExchangeId(String productExchangeId, String delFlag) {
        HashMap<Object, Object> map = new HashMap<>();
        if(!delFlag.equals("1")){
            map.put("code",  1);
            map.put("msg", "无法删除");
            return map;
        }
        productDetailsDbService.deleteProductExchangeFormByProductExchangeId(productExchangeId);
        map.put("code",  0);
        map.put("msg", "删除成功");
        return map;
    }

    @Override
    public List<ProductExchangeaApprovalVO> selectProductExchangeApprovaLByOrderNumber(String orderNumber) {
        return productDetailsDbService.selectProductExchangeApprovaLByOrderNumber(orderNumber);
    }

    @Override
    public void deleteRroductExchangeForm(String productExchangeId) {
        String[] split = productExchangeId.split(",");
        for (int i =0;i< split.length;i++){
            productDetailsDbService.deleteRroductExchangeForm(split[i]);
        }


    }

    @Override
    public List<CustomerProductVO> selectExchangeCustomerProductList(String productName) {
        return productDetailsDbService.selectExchangeCustomerProductList(productName);
    }

    @Override
    public List<CustomerMemberProductVO> selectExchangeCustomerMemberProductList(String memberCardName) {
        return productDetailsDbService.selectExchangeCustomerMemberProductList(memberCardName);
    }

    @Override
    public ProductExchangeaApprovalVO selectApprovedform(String productExchangeApprovalId) {
        return productDetailsDbService.selectApprovedform(productExchangeApprovalId);
    }


    private void addFinishOrderProduct(String changeCourseTitle, String courseProductCode, String accountId,
                                       String finishOrderProductId, String productId, String companyId) {
        //根据id查询Finish表
        FinishOrderProductVO vo = customerDbService.selectFinishOrderProductById(finishOrderProductId);
        //调用方法取uuid
        String guid = CommonUtils.getGUID();
        //获取当前时间
        Date date = new Date();
        String number = String.valueOf((int) ((Math.random() * 9 + 1) * 100));
        //新增一条数据到finish
        vo.setFinishOrderProductId(guid);
        vo.setFinishOrderProductNum(String.valueOf(new Date().getTime())+ number);//产品号
        vo.setProductId(productId);
        vo.setProductName(changeCourseTitle);
        vo.setProductCode(courseProductCode);
        vo.setOrderDetailType("4");
        vo.setProductStatus("7");
        vo.setCreatePeopleId(accountId);
        vo.setCompanyId(companyId);
        vo.setCreateTm(date);
        customerDbService.addFinishOrderProduct(vo);
    }


}
